import sys
import os
import random
import time
BASE_PATH = os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))
)
sys.path.insert(0,BASE_PATH)

from bin import get_Webdriver
from page_object.public import Public


class Plugins_Manage:
    def __init__(self):
        self.driver = get_Webdriver.Get_Webdriver().driver

    def jumptopluginspage(self):
        curl = Public().getCurrentUrl()
        if '/plugins/main.html' not in curl:
            self.driver.click("link_text=>数据分析工具")
            self.driver.click("link_text=>插件管理")
        else:
            self.driver.F5()

    def clearPluginsName(self):
        """清空输入的插件名称"""
        self.driver.clear("css=>#fileName")

    def uploadPlugins(self, name="", path=""):
        """上传插件 name=插件名称  path插件在本机的路径"""
        if name != "":
            self.driver.type("css=>#fileName", name)
        if path != "":
            self.driver.type("css=>.webuploader-element-invisible", path)
        self.driver.click("css=>#ctlBtn")

    def uploadMultipleFiles(self, name, *path):
        if name != "":
            self.driver.type("css=>#fileName", name)
        if path != "":
            for i in path:
                self.driver.type("css=>.webuploader-element-invisible", i)

    def clickUploadButton(self):
        """点击上传按钮"""
        self.driver.click("css=>#ctlBtn")

    def deleteUploadFile(self):
        """删除准备上传的文件"""
        # self.driver.click("css=>.webuploadDelbtn")
        self.driver.click("xpath=>.//*[@class='webuploadDelbtn']")

    def findPlugins(self, name):
        """根据插件名称查找插件 模糊查找"""
        self.driver.clear("css=>#search_word")
        self.driver.type("css=>#search_word", name)
        self.driver.click("css=>#search_btn")

    def getallPluginsCount(self):
        """获取当前条件下所有插件个数"""
        time.sleep(1)
        try:
            return int(self.driver.get_text('css=>.pagination-info').split(" ")[5])
        except IndexError:
            return "文件列表为空"

    def getPluginsCount(self):
        """获取当前页面插件个数"""
        time.sleep(1)
        try:
            return int(self.driver.get_text('css=>.pagination-info').split(" ")[3])
        except IndexError:
            return "文件列表为空"

    def getPluginsNames(self, counts):
        """获取当前页面所有插件名称"""
        if counts != "文件列表为空":
            pluginsNames = []
            for i in range(1, counts+1):
                fileName = self.driver.get_text("xpath=>.//*[@id='maingrid']/tbody/tr[{}]/td[2]".format(i))
                pluginsNames.append(fileName)
            return pluginsNames
        else:
            return "文件列表为空"

    def getPluginsrandom(self, pluginsCount):
        """从插件列表中随机选择出一个并返回名称和xpath序号"""
        xpathindex = random.randint(1, pluginsCount)
        fileName = self.driver.get_text("xpath=>.//*[@id='maingrid']/tbody/tr[{}]/td[2]".format(xpathindex))
        print(fileName)
        return fileName, xpathindex

    def deletePluginsByindex(self, index):
        """点击后方删除按钮"""
        self.driver.click("xpath=>.//*[@id='maingrid']/tbody/tr[{}]/td[7]/div/a/i".format(index))

    def getPreviewFileName(self):
        """获取文件名预览"""
        filename = self.driver.get_text("css=>.info")
        return filename

    def getElement(self):
        """查看文件是否存在"""
        try:
            self.driver.get_element("css=>.info")
        except:
            return ''

    # def getMsgTitle(self):
    #     """获取提示信息标题"""
    #     WebDriverWait(self.dr, 5).until(lambda driver: driver.find_element_by_xpath(ReadConfig().MsgTitle_text_xpath))
    #     return self.dr.find_element_by_xpath(ReadConfig().MsgTitle_text_xpath).text
    #
    # def getMsgContent(self):
    #     """获取提示信息内容"""
    #     WebDriverWait(self.dr, 5).until(lambda driver: driver.find_element_by_xpath(ReadConfig().MsgContent_text_xpath))
    #     return self.dr.find_element_by_xpath(ReadConfig().MsgContent_text_xpath).text

    def clickNextPageButton(self):
        """点击翻页按钮"""
        try:
            self.dr.find_element_by_xpath(".//*[@class='page-next']/a").click()
        except:
            print("未找到下一页按钮 或者 当前列表不足一页")


if __name__ == '__main__':
  pass